package com.baizhi;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

@SpringBootTest
public class Sms {

    @Test
    public void  SmsTest() throws JsonProcessingException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GFstt8zYBtJ4RtMnz5x", "BLg798993");
        IAcsClient client = new DefaultAcsClient(profile);
        //构建请求
        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//不动
        request.setSysVersion("2017-05-25");//不动
        request.setSysAction("SendSms");

        request.putQueryParameter("PhoneNumbers", "15919166087");//手机号
        request.putQueryParameter("SignName", "机器人时代");//签名，从自己的阿里云获取
        request.putQueryParameter("TemplateCode", "SMS_206736269");//短信模板，从自己阿里云获取
        //构建一个短信验证码
        ObjectMapper om=new ObjectMapper();
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",5946);
        request.putQueryParameter("TemplateParam", om.writeValueAsString(map));//短信模板，从自己阿里云获取

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
